package de.urlaub.client.data;

import java.util.ArrayList;
import java.util.HashMap;

import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import de.urlaub.client.logic.VacationPanelUtil;
import de.urlaub.server.comm.ApprovalMailSender;
import de.urlaub.server.comm.ApprovalMailSenderRemote;
import de.urlaub.server.logging.LoggingInterceptor;

@Stateless
@Interceptors(LoggingInterceptor.class)
public class VacationDaoImpl implements VacationDao {
	@PersistenceContext(unitName="examplePU")
	private EntityManager entityManager;

	@Override
	public void persistEmployee(Employee employee) {
		entityManager.persist(employee);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Employee> getEmployees() {
		ArrayList<Employee> employees = new ArrayList<Employee>();
		
		Query query = entityManager.createNativeQuery("Select * From Employee", Employee.class);
		employees = (ArrayList<Employee>) query.getResultList();
		
		return employees;
	}

	@Override
	public void persistDivision(Division division) {
		entityManager.persist(division);
	}

	@Override
	public Division getDivision(int divisionId) {
		return entityManager.find(Division.class, divisionId);
	}

	@Override
	public void persistVacation(Vacation vacation) {
		entityManager.persist(vacation);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Vacation> getVacations(int state) {
		Query query = entityManager.createQuery("Select v From Vacation v Where v.state = :state", Vacation.class);
		query.setParameter("state", state);
		
		return (ArrayList<Vacation>) query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Vacation> getVacations(Employee employee, int state) {
		Query query = entityManager.createQuery("Select v From Vacation v Where v.state = :state And v.employee = :employee",
												Vacation.class);
		query.setParameter("employee", employee);
		query.setParameter("state", state);
		
		return (ArrayList<Vacation>) query.getResultList();
	}
	
	@Override
	public Vacation getVacation(int vacationId)
	{
		Query query = entityManager.createQuery("Select v From Vacation v Where v.vacationId = :vacationId", 
												Vacation.class);
		query.setParameter("vacationId", vacationId);
		return (Vacation) query.getSingleResult();
	}

	@Override
	public void updateVacations(HashMap<Integer, Boolean> changedVacations) {
		try {
			entityManager.getTransaction().begin();
			for (Integer vacationId : changedVacations.keySet())
			{
				boolean isApproved = changedVacations.get(vacationId);
				Query query = entityManager.createQuery("Update Vacation Set state = :state Where vacationId = :vacationId");
				query.setParameter("vacationId", vacationId);
				query.setParameter("state", isApproved ? 2 : 1);
				query.executeUpdate();
			}
			entityManager.getTransaction().commit();
		}
		catch (Exception e) {
			System.out.println("Fehler beim Abwickeln der Transaktion");
		}
	}
}
